package com.hbnu.test.www.net;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressDemo {
    public static void main(String[] args) {

        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            System.out.println(inetAddress.toString());  // 获取主机信息
            System.out.println(inetAddress.getHostName());  // 获取主机名
            System.out.println(inetAddress.getHostAddress()); // 获取主机地址(ip)

            InetAddress localhost = InetAddress.getByName("localhost");
            System.out.println(localhost.toString());
            System.out.println(localhost.getHostName());
            System.out.println(localhost.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
